package com.mask.token.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.Instant;
import java.util.Collection;
import java.util.Collections;

/**
 * Mask用户详情
 * 实现Spring Security的UserDetails接口
 *
 * @author mask
 * @since 1.0.0
 */
@Data
public class MaskUserDetails implements UserDetails {

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * 是否未过期
     */
    private boolean accountNonExpired = true;

    /**
     * 是否未锁定
     */
    private boolean accountNonLocked = true;

    /**
     * 凭据是否未过期
     */
    private boolean credentialsNonExpired = true;

    /**
     * 权限列表
     */
    private Collection<? extends GrantedAuthority> authorities = Collections.emptyList();

    /**
     * 设备ID
     */
    private String deviceId;

    /**
     * 登录时间
     */
    private Instant loginTime;

    /**
     * 最后活跃时间
     */
    private Instant lastActiveTime;

    /**
     * 用户角色
     */
    private String role;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 用户手机号
     */
    private String phone;

    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 扩展属性
     */
    private java.util.Map<String, Object> attributes = new java.util.HashMap<>();

    public MaskUserDetails() {
        this.loginTime = Instant.now();
        this.lastActiveTime = Instant.now();
    }

    public MaskUserDetails(Long userId, String username, String password) {
        this();
        this.userId = userId;
        this.username = username;
        this.password = password;
    }

    /**
     * 更新最后活跃时间
     */
    public void updateLastActiveTime() {
        this.lastActiveTime = Instant.now();
    }

    /**
     * 添加扩展属性
     */
    public void addAttribute(String key, Object value) {
        this.attributes.put(key, value);
    }

    /**
     * 获取扩展属性
     */
    public Object getAttribute(String key) {
        return this.attributes.get(key);
    }

    /**
     * 添加权限
     */
    public void addAuthority(String authority) {
        java.util.List<GrantedAuthority> authorityList = new java.util.ArrayList<>();
        if (this.authorities != null && !this.authorities.isEmpty()) {
            authorityList.addAll(this.authorities);
        }
        authorityList.add(() -> authority);
        this.authorities = authorityList;
    }

    /**
     * 设置用户代理
     */
    public void setUserAgent(String userAgent) {
        addAttribute("userAgent", userAgent);
    }

    /**
     * 设置登录IP
     */
    public void setLoginIp(String loginIp) {
        addAttribute("loginIp", loginIp);
    }

    /**
     * 获取Token（用于当前用户）
     */
    public String getToken() {
        return (String) getAttribute("token");
    }

    /**
     * 设置Token
     */
    public void setToken(String token) {
        addAttribute("token", token);
    }
}
